package com.soft.mapper;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.soft.entity.Money;

@SpringBootTest
public class MoneyMapperTest {
	@Autowired
	private MoneyMapper moneyMapper;
	@Test
	public void testSelect() {
		List<Money> moneyList = moneyMapper.selectList(null);
		for(int i=0;i<moneyList.size();i++)
		{
			System.out.println(moneyList.get(i).getId());
			System.out.println(moneyList.get(i).getIncome());
			System.out.println(moneyList.get(i).getSpend());
		}
	}
	@Test
	public void selectOneTest() {
		Money money = moneyMapper.selectById(1l);
		System.out.println(money.getNote());
	}
	@Test
	public void selectTweTest() {
		QueryWrapper<Money> qw = new QueryWrapper<>();
		qw.gt("money_income", 1);
		try {
		Money money = moneyMapper.selectOne(qw);
		System.out.println(money.getNote());
		}catch(Exception e) {
			System.out.println("too many date");
		}
	}
	//selectOne查询只能查一条，多出会报错，可以用catch抛出
	//可以反映出是一条还是多条记录
	@Test
	public void testInsert() {
		Money money=new Money();
		money.setIncome(new BigDecimal("123"));
        money.setSpend(new BigDecimal("45"));
        money.setTime(LocalTime.now());
        money.setNote("测试插入");
        money.setTotal(new BigDecimal("78"));
	}
	@Test
	public void updateTest() {
		Money money=new Money();
		money.setId(1l);
		money.setIncome(new BigDecimal("125"));
        money.setSpend(new BigDecimal("55"));
        money.setTime(LocalTime.now());
        money.setNote("测试更新");
        money.setTotal(new BigDecimal("70"));
		moneyMapper.updateById(money);
	}
	@Test
	public void updateQueryTest() {
		UpdateWrapper<Money> uw = new UpdateWrapper<>();
		uw.gt("money_income", 125);
		Money money = new Money();
		money.setNote("查询更新测试");
		moneyMapper.update(money,uw);
	}
	@Test
	public void deleteTest() {
		Money money=new Money();
		money.setId(2l);
		moneyMapper.deleteById(money);
	}
	@Test
	public void deleteQueryTest() {
		QueryWrapper<Money> qw = new QueryWrapper<>();
		qw.eq("money_note", "测试更新");
		moneyMapper.delete(qw);
	}
}
